﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Jixiao
{
    internal class ProjectListInfo
    {
        public static List<TaskItem> FilterItemsByUserName(string userName, List<ListViewItem> m_allItems, List<string> outProjectNameList,string filterYearMonth)
        {
            if (string.IsNullOrEmpty(userName))
            {
                MessageBox.Show("请选择导出人");
                return null;
            }
            var list = new List<TaskItem>();
            List<string> projectList = new List<string>();
            for (var i = 0; i < m_allItems.Count; i++)
            {
                TaskItem item = m_allItems[i].Tag as TaskItem;
                var t1 = (string.IsNullOrEmpty(userName) || item.MyName == userName);
                if (!t1)
                {
                    continue;
                }
                var c1 = (item.StartTime.Year + "年" + (item.StartTime.Month) + "月");
                var c2 = (item.EndTime.Year + "年" + (item.EndTime.Month) + "月");
                var t = c1 == filterYearMonth || c2 == filterYearMonth;
                var t3 = item.ProjectName == "日常工作" || item.ProjectName == "协同工作";
                if (t || t3)
                {
                    list.Add(item);
                    projectList.Add(item.ProjectName);
                }
            }
            outProjectNameList.AddRange(projectList.Distinct().ToList());
            return list;
        }
        public static List<string> FilterItemsByUserName(string userName,string filterYearMonth)
        {
            if (string.IsNullOrEmpty(userName))
            {
                MessageBox.Show("请选择导出人");
                return null;
            }
            var connection = Form1.Connection;
            var dt = connection.SelectData($"select * from TaskList where myName=\"{userName}\"");
            if (dt == null)
            {
                return null;
            }
            List<string> projectList = new List<string>();

            for (var i = 0; i < dt.Rows.Count; i++)
            {
                TaskItem item = TaskItem.FromRow(dt.Rows[i]);

                var c1 = (item.StartTime.Year + "年" + (item.StartTime.Month) + "月");
                var c2 = (item.EndTime.Year + "年" + (item.EndTime.Month) + "月");

                if (c1 == filterYearMonth || c1 == filterYearMonth || c2 == filterYearMonth || c2 == filterYearMonth)
                    projectList.Add(item.ProjectName);
            }
            connection.Dispose();
            return projectList.Distinct().ToList();
        }
    }
}
